#include <rtthread.h>
#include "calc.h"
#include "sys_conf.h"
#include "local_status.h"
#include "req_execution.h"
#include "sys_status.h"
#include "dio_bsp.h"
#include "rtc_bsp.h"
#include <stdlib.h>
#include <math.h>
#include "global_var.h"
#include "pwm_bsp.h"
#include "usart_bsp.h"
#include "calc.h"

#define CONFIG_DEBUG
#ifdef CONFIG_DEBUG
#ifndef req_exe_log
#define req_exe_log(N, ...) rt_kprintf("[%d][req_exe:%04d]" N "\r\n", rt_tick_get(), __LINE__, ##__VA_ARGS__)
#endif /* req_exe_log(...) */
#else
#define req_exe_log(...)
#endif /* ! CONFIG_DEBUG */

enum
{
    RUNING_STATUS_COOLING_BPOS = 0,
    RUNING_STATUS_HEATING_BPOS,
    RUNING_STATUS_HUMIDIFYING_BPOS,
    RUNING_STATUS_DEHUMING_BPOS,
};

enum
{
    COMPRESSOR_SIG_HOLD = 0,
    COMPRESSOR_SIG_ON,
    COMPRESSOR_SIG_OFF,
    COMPRESSOR_SIG_ERR,
};

enum
{
    FAN_SIG_IDLE = 0,
    FAN_SIG_START,
    FAN_SIG_STOP
};

enum
{
    FAN_TPYE_AC = 0,
    FAN_TPYE_EC
};

typedef struct
{
    uint16_t time_out;
    uint16_t flush_delay_timer;
    uint16_t hum_fill_cnt;
    uint32_t hum_timer;
    uint32_t check_timer;
    uint8_t check_fill_flag;
    uint8_t check_drain_flag;
    uint8_t check_flag;
    uint16_t warm_time;
} hum_timer;

// static hum_timer hum_delay_timer;

extern sys_reg_st g_sys;
extern local_reg_st l_sys;

//需求比特位操作函数
void req_bitmap_op(uint8_t component_bpos, uint8_t action)
{
    uint8_t byte_offset, bit_offset;
    byte_offset = component_bpos >> 4;
    bit_offset  = component_bpos & 0x0f;

    if (action == 0)
    {
        l_sys.bitmap[byte_offset][BITMAP_REQ] &= ~(0x0001 << bit_offset);
    }
    else
    {
        l_sys.bitmap[byte_offset][BITMAP_REQ] |= (0x0001 << bit_offset);
    }

    //			if(action == 0)
    //			{
    //					l_sys.bitmap[BITMAP_REQ] &= ~(0x0001<<component_bpos);
    //			}
    //			else
    //			{
    //					l_sys.bitmap[BITMAP_REQ] |= (0x0001<<component_bpos);
    //			}
}

//需求模拟输出操作函数
static void req_ao_op(uint8_t component_bpos, int16_t value)
{
    if ((g_sys.config.ComPara.u16Manual_Test_En == 0) && (g_sys.config.ComPara.u16Test_Mode_Type == 0))
    {  // normal mode
        switch (component_bpos)
        {
            case (AO_EC_FAN): {
                if (1)
                {
                    l_sys.ao_list[component_bpos][BITMAP_REQ] = value;
                }
                break;
            }
            case (AO_EC_COMPRESSOR): {
                if (1)
                {
                    l_sys.ao_list[component_bpos][BITMAP_REQ] = value;
                }
                break;
            }
            default: {
                l_sys.ao_list[component_bpos][BITMAP_REQ] = value;
                break;
            }
        }
    }
    else
    {  // dianose or test mode, output directly
        l_sys.ao_list[component_bpos][BITMAP_REQ] = value;
    }
}

// static void req_pwm_op(uint8_t component_bpos, int16_t value)
//{
//

//		l_sys.pwm_list[component_bpos][BITMAP_REQ] = value;
//}
//模拟输出跟踪函数，向设置目标，按照步长参数进行变化；
static int16_t analog_step_follower(int16_t target, uint16_t dev_type)
{
    uint16_t current_output;
    int16_t ret_val;
    int16_t delta;

    switch (dev_type)
    {
        case (AO_EC_FAN): {
            current_output = g_sys.status.aout[AO_EC_FAN];
            delta          = target - current_output;
            if (abs(delta) > g_sys.config.fan.adjust_step)
            {
                if (delta > 0)
                    ret_val = current_output + g_sys.config.fan.adjust_step;
                else
                    ret_val = current_output - g_sys.config.fan.adjust_step;
            }
            else if (delta == 0)
            {
                ret_val = current_output;
            }
            else
            {
                ret_val = current_output + delta;
            }
            break;
        }
        default: {
            ret_val = target;
            break;
        }
    }
    return ret_val;
}

void Close_DIS_PWR(uint8_t u8Type)
{
    if (u8Type)
    {
        req_bitmap_op(DO_PWR_CTRL_BPOS, 1);  //关闭显示电源
    }
    else
    {
        req_bitmap_op(DO_PWR_CTRL_BPOS, 0);  //关闭显示电源
    }
    return;
}

//风机档位输出
static void Fan_Fsm_Out(uint8_t Fan_Gear)
{
    switch (Fan_Gear)
    {
        case FAN_GEAR_START: {
            req_bitmap_op(DO_FAN_LOW_BPOS, 1);
            req_bitmap_op(DO_FAN_MID_BPOS, 0);
            break;
        }
        case FAN_GEAR_MID: {
            req_bitmap_op(DO_FAN_LOW_BPOS, 0);
            req_bitmap_op(DO_FAN_MID_BPOS, 1);
            break;
        }
        case FAN_GEAR_NO: {
            req_bitmap_op(DO_FAN_LOW_BPOS, 0);
            req_bitmap_op(DO_FAN_MID_BPOS, 0);
            break;
        }
        default: {
            req_bitmap_op(DO_FAN_LOW_BPOS, 0);
            req_bitmap_op(DO_FAN_MID_BPOS, 0);
            break;
        }
    }
}

/**
 * @brief 	fan output control state FSM execution
 * @param  none
 * @retval none
 */
//风机状态机执行函数
static void fan_fsm_exe(uint8_t fan_signal)
{
    uint16_t u16Test;

    switch (l_sys.l_fsm_state[FAN_FSM_STATE])
    {
        case (FSM_FAN_IDLE): {
            if (fan_signal == FAN_SIG_START)
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_INIT;
                //                l_sys.comp_timeout[DO_FAN_BPOS] =g_sys.config.ComPara.u16Start_Delay;  // assign
                //                startup delay to timeout counter
                l_sys.comp_timeout[DO_FAN_BPOS] = 0;  // assign startup delay to timeout counter
            }
            else
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_IDLE;
                l_sys.comp_timeout[DO_FAN_BPOS]  = l_sys.comp_timeout[DO_FAN_BPOS];  // remains timeout counter
            }
            l_sys.Fan.Fan_Gear[0] = FAN_GEAR_NO;  //无输出						//disable fan output
            l_sys.Fan.Fan_Gear[1] = FAN_GEAR_NO;  //无输出						//disable fan output
            break;
        }
        case (FSM_FAN_INIT): {
            if (fan_signal != FAN_SIG_START)
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_IDLE;
                l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_NO;                      //无输出
                l_sys.comp_timeout[DO_FAN_BPOS]  = l_sys.comp_timeout[DO_FAN_BPOS];  // reset timeout counter
            }
            else
            {
                if (l_sys.comp_timeout[DO_FAN_BPOS] == 0)
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_START_UP;
                    l_sys.comp_timeout[DO_FAN_BPOS]  = 0;
                    l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_START;  //
                }
                else  // wait until startup delay elapses
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_INIT;
                    l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_NO;  //无输出
                }
            }
            break;
        }
        case (FSM_FAN_START_UP): {
            if (l_sys.comp_timeout[DO_FAN_BPOS] == 0)
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_NORM;
            }
            else
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_START_UP;
            }
            l_sys.Fan.Fan_Gear[0]           = FAN_GEAR_START;                   //
            l_sys.comp_timeout[DO_FAN_BPOS] = l_sys.comp_timeout[DO_FAN_BPOS];  // remain timeout counter
            break;
        }
        case (FSM_FAN_NORM): {
            if ((fan_signal == FAN_SIG_STOP) && (l_sys.comp_timeout[DO_FAN_BPOS] == 0))
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_SHUT_CFAN;
                //							l_sys.comp_timeout[DO_FAN_BPOS] = g_sys.config.ComPara.u16Fan_Stop_Delay;
                ////assign startup delay to timeout counter
                l_sys.comp_timeout[DO_CFAN_BPOS] = DELAY_CFAN;  // reset timeout counter
            }
            else
            {
                u16Test                          = 1;
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_NORM;
                //                l_sys.Fan.Fan_Gear[1] = FAN_GEAR_START;  //
                if ((g_sys.status.ComSta.u16TH[0].Hum < LHumidity) &&
                    (l_sys.Fan.Fan_Gear[0] == FAN_GEAR_START))  //大风量
                {
                    u16Test |= 0x02;
                    l_sys.Fan.Fan_Delay++;
                    if (l_sys.Fan.Fan_Delay >= DELAY_FAN_GEAR * OFFSET_DELAY)
                    {
                        u16Test |= 0x04;
                        l_sys.Fan.Fan_Delay   = 0;
                        l_sys.Fan.Fan_Gear[0] = FAN_GEAR_MID;  //
                    }
                }
                else if ((g_sys.status.ComSta.u16TH[0].Hum > HHumidity) &&
                         (l_sys.Fan.Fan_Gear[0] == FAN_GEAR_MID))  //降低风量
                {
                    u16Test |= 0x08;
                    l_sys.Fan.Fan_Delay++;
                    if (l_sys.Fan.Fan_Delay >= DELAY_FAN_GEAR * OFFSET_DELAY)
                    {
                        u16Test |= 0x10;
                        l_sys.Fan.Fan_Delay   = 0;
                        l_sys.Fan.Fan_Gear[0] = FAN_GEAR_START;  //
                    }
                }
                else
                {
                    u16Test |= 0x20;
                    l_sys.Fan.Fan_Delay   = 0;
                    l_sys.Fan.Fan_Gear[0] = l_sys.Fan.Fan_Gear[0];
                }
                //静音模式
                if (g_sys.config.ComPara.LN.u16LN_Enable)
                {
                    u16Test |= 0x40;
                    l_sys.Fan.Fan_Gear[0] = FAN_GEAR_START;  //
                }
                //待机模式
                if (l_sys.u8Sys_State == FSM_IDLE)
                {
                    u16Test |= 0x80;
                    l_sys.Fan.Fan_Gear[0] = FAN_GEAR_START;  //
                }
                l_sys.Fan.Fan_Gear[1] = l_sys.Fan.Fan_Gear[0];
            }
            //						rt_kprintf("Fan_Gear[0] = %d,fan_signal = %d,Fan_Delay = %d,Hum =%d,Mute = %d,Hum
            //=%d,u16LN_Enable
            //=%x\n",l_sys.Fan.Fan_Gear[0],fan_signal,l_sys.Fan.Fan_Delay,g_sys.status.ComSta.u16TH[0].Hum,l_sys.LedKey.Mute,g_sys.config.ComPara.LN.u16LN_Enable,u16Test);
            break;
        }
        case (FSM_FAN_SHUT_CFAN): {
            if (fan_signal == FAN_SIG_START)
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_NORM;
                l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_START;  //
            }
            else
            {
                if (l_sys.comp_timeout[DO_CFAN_BPOS] == 0)
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_SHUT_DHFAN;
                    l_sys.Fan.Fan_Gear[1] =
                        FAN_GEAR_NO;                                //																			//enable
                                                                    // fan output
                    l_sys.comp_timeout[DO_FAN_BPOS] = DELAY_DHFAN;  // reset timeout counter
                }
                else  // wait until startup delay elapses
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_SHUT_CFAN;
                }
            }
            break;
        }
        case (FSM_FAN_SHUT_DHFAN): {
            if (fan_signal == FAN_SIG_START)
            {
                l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_NORM;
                l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_START;  //
            }
            else
            {
                if (l_sys.comp_timeout[DO_FAN_BPOS] == 0)
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_IDLE;
                    l_sys.Fan.Fan_Gear[0] =
                        FAN_GEAR_NO;  //																			//enable
                }
                else  // wait until startup delay elapses
                {
                    l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_SHUT_DHFAN;
                }
            }
            break;
        }
        default: {
            l_sys.l_fsm_state[FAN_FSM_STATE] = FSM_FAN_IDLE;
            l_sys.Fan.Fan_Gear[0]            = FAN_GEAR_NO;  //
            l_sys.Fan.Fan_Gear[1]            = FAN_GEAR_NO;
            l_sys.comp_timeout[DO_FAN_BPOS]  = 0;  // reset timeout counter
            break;
        }
    }
    Fan_Fsm_Out(l_sys.Fan.Fan_Gear[0]);  //风机DO输出
    if (l_sys.Fan.Fan_Gear[1])
    {
        req_bitmap_op(DO_CFAN_BPOS, 1);
    }
    else
    {
        req_bitmap_op(DO_CFAN_BPOS, 0);
    }
    return;
}

#define EC_FAN_NOLOAD_DELAY 20
void ec_fan_output(int16_t req_temp, int16_t req_hum, uint8_t fan_sig)
{
    uint16_t status_sense;
    uint16_t require;
    uint16_t fan_mode;
    uint16_t u16Fan_Speed;
    // static uint16_t ec_fan_shut_delay = 0;

    fan_mode = g_sys.config.fan.mode;
    //#ifdef SYS_M_V50
    if ((g_sys.config.ComPara.LN.u16LN_Mode) || (g_sys.config.ComPara.LN.u16LN_Enable))  //低噪音,低速制水
    {
        u16Fan_Speed = g_sys.config.ComPara.LN.u16LN_Fan;
    }
    else
    {
        u16Fan_Speed = g_sys.config.fan.set_speed;
    }
    //#else
    //		u16Fan_Speed = g_sys.config.fan.set_speed;
    //#endif
    require = 0;

    {
        if (l_sys.Fan.Fan_Gear[0] == FAN_GEAR_NO)  //未开风机
        {
            require = 0;
        }
        else if (g_sys.config.fan.type == FAN_TPYE_EC)
        {
            if (l_sys.l_fsm_state[FAN_FSM_STATE] == FSM_FAN_START_UP)
            {
                require = u16Fan_Speed;
            }
            else
            {
                // fan mode in invertor compressor mode
                if (fan_mode == FAM_MODE_INV_COMP)  //变速
                {
                    require = analog_step_follower(req_temp, AO_EC_FAN);
                    // ec_fan_shut_delay = EC_FAN_NOLOAD_DELAY;
                }
                else if (fan_mode == FAN_MODE_FIX)  //定速
                {
                    require = u16Fan_Speed;
                }
                else
                {
                    if ((g_sys.config.fan.noload_down != 0) && (status_sense == 0))
                    {
                        require = u16Fan_Speed;
                    }
                    else
                    {
                        require = u16Fan_Speed;
                    }
                }
            }
            require = lim_min_max(g_sys.config.fan.min_speed, g_sys.config.fan.max_speed, require);
        }
        else
        {
            require = u16Fan_Speed;
        }
    }
    req_ao_op(AO_EC_FAN, require);
}

/**************************************/
//风机状态机信号产生电路
static uint8_t fan_signal_gen(void)
{
    uint8_t fan_signal;

    fan_signal = 0;
    if ((sys_get_pwr_sts() == 1) && (l_sys.u8Sys_State != FSM_STOP))
    {
        fan_signal = FAN_SIG_START;
    }
    else if ((sys_get_pwr_sts() == 0) || (l_sys.u8Sys_State == FSM_STOP))
    {
        if ((g_sys.status.ComSta.u16Dout_bitmap[0] & ((0x0001 << DO_RH2_BPOS))) == 0)
        {
            fan_signal = FAN_SIG_STOP;
        }
        else
        {
            fan_signal = FAN_SIG_IDLE;
        }
    }
    else
    {
        fan_signal = FAN_SIG_IDLE;
    }
    //    rt_kprintf("fan_signal=%x,Fan_Close=%d\n",fan_signal,l_sys.Fan.Fan_Close[0]);
    return fan_signal;
}

//风机控制
void fan_req_exe(int16_t target_req_temp, int16_t target_req_hum)
{
    uint8_t fan_signal;
    fan_signal = fan_signal_gen();  //风机状态机信号产生
    fan_fsm_exe(fan_signal);        //风机状态机执行
    //    ec_fan_output(target_req_temp, target_req_hum, fan_signal); //风机模拟输出控制
}

static PID_Para PID_Temp;
/*====================================================================================================
   Initialize PID Structure
=====================================================================================================*/

void PIDInit(void)
{
    PID_Temp.err      = 0;
    PID_Temp.err_last = 0;
    PID_Temp.err_next = 0;
}

int16_t PID_control(int16_t target_req_temp)
{
    int16_t adjust;
    float inc;  // inc
    int inc1;   //对inc四舍五入取整
                //	if(pid_control_time>=PID_CAL_TIME)
    {
        PID_Temp.Kp  = g_sys.config.Heater.PID[0];             //  Set PID Coefficients
        PID_Temp.Ki  = g_sys.config.Heater.PID[1];             //  Set PID Coefficients
        PID_Temp.Kd  = g_sys.config.Heater.PID[2];             //  Set PID Coefficients
        PID_Temp.Set = g_sys.config.Heater.DesorptionTemp[2];  //  Set PID Setpoint
        //     rt_kprintf("Kp=%d,Set=%d,temp=%d,DT=%d\n",PID_Temp.Kp,PID_Temp.Set,target_req_temp,g_sys.config.Heater.DesorptionTemp[2]);
        PID_Temp.err = PID_Temp.Set - target_req_temp;  // 偏差
        if (abs(PID_Temp.err) < 1)                      // 0.1度
        {
            adjust = 0;
        }
        else
        {
            inc = PID_Temp.Kp * (PID_Temp.err - PID_Temp.err_next) + PID_Temp.Ki * PID_Temp.err +
                  PID_Temp.Kd * (PID_Temp.err - 2 * PID_Temp.err_next + PID_Temp.err_last);  //计算控制增量
            PID_Temp.err_last = PID_Temp.err_next;
            PID_Temp.err_next = PID_Temp.err;
            inc1              = (int)inc;
            if (inc > 0)
            {
                if (inc - inc1 >= 50)
                {
                    inc1 += 100;
                }
            }
            if (inc < 0)
            {
                if (inc1 - inc >= 50)
                {
                    inc1 -= 100;
                }
            }
            inc1 /= 100;  //放大了100倍
            adjust = (int16_t)inc1;
            //		 rt_kprintf("Kp=%d,Set=%d\n",PID_Temp.Kp,PID_Temp.Set);
        }
        rt_kprintf("adjust=%d,PID_Temp.err=%d,inc=%d,inc1=%d,temp=%d,Set=%d\n", adjust, PID_Temp.err, inc, inc1,
                   target_req_temp, PID_Temp.Set);
        //		if(adjust<=0)
        //			adjust=0;
    }
    return adjust;
}

//热脱附
void Heat_req_exe(int16_t target_req_temp, int16_t target_req_hum)
{
    uint8_t Heat_signal = FALSE;
    int16_t i16CurTemp  = (int16_t)(g_sys.status.ComSta.u16Ain[AI_NTC_RW2]);
    //		int16_t Geardelay=g_sys.config.ComPara.u16CloseDelay;
    int16_t i16HeatAO          = 0;
    int16_t i16PIDinc          = 0;
    static uint8_t u8HeatDelay = 0;

    if ((sys_get_pwr_sts() == 1) && (l_sys.u8Sys_State == FSM_RUN))
    {
        Heat_signal = TRUE;
    }
    else
    {
        Heat_signal = FALSE;
    }
    //     rt_kprintf("HEATER_FSM_STATE=%x,Heat_signal=%d,Heat_Gear=%d,i16CurTemp=%d\n",l_sys.l_fsm_state[HEATER_FSM_STATE],Heat_signal,l_sys.Heat.Heat_Gear,i16CurTemp);
    switch (l_sys.l_fsm_state[HEATER_FSM_STATE])
    {
        case (HEATER_FSM_IDLE): {
            if ((Heat_signal == TRUE) && (l_sys.l_fsm_state[FAN_FSM_STATE] == FSM_FAN_NORM) &&
                (sys_get_remap_status(WORK_MODE_STS_REG_NO, FAN_STS_BPOS) == 1))  // fan disabled, emergency shutdown
            {
                l_sys.l_fsm_state[HEATER_FSM_STATE] = HEATER_FSM_NOR;
            }
            req_bitmap_op(DO_RH2_BPOS, 0);
            req_ao_op(AO_HEATER, HEATER_OUT_ZERO);
            break;
        }
        case (HEATER_FSM_NOR): {
            if (Heat_signal == FALSE)
            {
                l_sys.l_fsm_state[HEATER_FSM_STATE] = HEATER_FSM_SHUT;
            }
            else
            {
                i16HeatAO = g_sys.status.ComSta.u16AO[0];
                u8HeatDelay++;
                if (u8HeatDelay >= (g_sys.config.Heater.PID_T * OFFSET_DELAY))
                {
                    u8HeatDelay = 0;
                    i16PIDinc   = PID_control(i16CurTemp);
                    i16HeatAO += i16PIDinc;
                }
                if (i16HeatAO >= 100)
                {
                    i16HeatAO = 100;
                }
                else if (i16HeatAO <= 0)
                {
                    i16HeatAO = 0;
                }
                //出热水，关闭加热
                if (l_sys.OutWater_Flag == WATER_HEAT)
                {
                    req_bitmap_op(DO_RH2_BPOS, 0);
                    req_ao_op(AO_HEATER, HEATER_OUT_ZERO);
                }
                else
                {
                    req_bitmap_op(DO_RH2_BPOS, 1);
                    req_ao_op(AO_HEATER, i16HeatAO);
                }
                g_sys.status.ComSta.REQ_TEST[0] = i16HeatAO;
                //									req_exe_log("u16AO=%d,i16PIDinc=%d,i16HeatAO=%d,REQ_TEST[0]=%d",
                // g_sys.status.ComSta.u16AO[0], i16PIDinc,i16HeatAO, g_sys.status.ComSta.REQ_TEST[0]);
            }
            break;
        }
        case (HEATER_FSM_SHUT): {
            if (Heat_signal == FALSE)
            {
                l_sys.l_fsm_state[HEATER_FSM_STATE] = HEATER_FSM_IDLE;
            }
            else
            {
                l_sys.l_fsm_state[HEATER_FSM_STATE] = HEATER_FSM_NOR;
            }
            break;
        }
        default: {
            l_sys.l_fsm_state[HEATER_FSM_STATE] = HEATER_FSM_IDLE;
            break;
        }
    }

    //    Heat_signal = Heat_signal_gen();                              //
    //    fan_fsm_exe(Heat_signal);                                    //
}

static uint16_t compressor_signal_gen(int16_t req_temp, int16_t req_hum, uint8_t *comp1_sig, uint8_t *comp2_sig)
{
    uint8_t comp1_alarm_flag, comp2_alarm_flag;
    uint16_t compressor_count;

    if ((g_sys.config.general.alarm_bypass_en == 0) &&
        ((l_sys.bitmap[0][BITMAP_REQ] & (0x0001 << DO_COMP1_BPOS)) != 0) &&
        ((l_sys.bitmap[0][BITMAP_ALARM] & (0x0001 << DO_COMP1_BPOS)) == 0) &&
        ((l_sys.bitmap[0][BITMAP_MASK] & (0x0001 << DO_COMP1_BPOS)) != 0))
        comp1_alarm_flag = 1;
    else
        comp1_alarm_flag = 0;

    if ((g_sys.config.general.alarm_bypass_en == 0) &&
        ((l_sys.bitmap[0][BITMAP_REQ] & (0x0001 << DO_COMP2_BPOS)) != 0) &&
        ((l_sys.bitmap[0][BITMAP_ALARM] & (0x0001 << DO_COMP2_BPOS)) == 0) &&
        ((l_sys.bitmap[0][BITMAP_MASK] & (0x0001 << DO_COMP2_BPOS)) != 0))
        comp2_alarm_flag = 1;
    else
        comp2_alarm_flag = 0;

    compressor_count = devinfo_get_compressor_cnt();

    if (sys_get_do_sts(DO_FAN_BPOS) == 0)  // fan disabled, emergency shutdown
    {
        *comp1_sig                                = COMPRESSOR_SIG_ERR;
        *comp2_sig                                = COMPRESSOR_SIG_ERR;
        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
        return 0;
    }

    if ((sys_get_pwr_sts() == 0) || ((l_sys.Comp_Close[0] != 0x00) && (l_sys.Comp_Close[1] != 0x00)))
    {
        *comp1_sig                                = COMPRESSOR_SIG_OFF;
        *comp2_sig                                = COMPRESSOR_SIG_OFF;
        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
        return 0;
    }

    if ((sys_get_remap_status(WORK_MODE_STS_REG_NO, FAN_STS_BPOS) != 0))
    {
        if (compressor_count == 1)  // one compressor configured
        {
            if (get_alarm_bitmap(ACL_E1) || get_alarm_bitmap(ACL_E2))  //告警
            {
                *comp1_sig = COMPRESSOR_SIG_OFF;
                *comp2_sig = COMPRESSOR_SIG_OFF;
            }
            else
            {
                *comp1_sig = COMPRESSOR_SIG_ON;
                *comp2_sig = COMPRESSOR_SIG_OFF;
            }
            l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
        }
        else if (compressor_count == 2)  // two compressors configured
        {
            switch (l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE])
            {
                case (0): {
                    *comp1_sig = COMPRESSOR_SIG_OFF;
                    *comp2_sig = COMPRESSOR_SIG_OFF;
                    if ((get_alarm_bitmap(ACL_E1) || get_alarm_bitmap(ACL_E2)) ||  //水满告警
                        ((comp1_alarm_flag & comp2_alarm_flag) != 0) ||            //告警
                        ((sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST1_STS_BPOS) != 0) &&
                         (sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST2_STS_BPOS) != 0)))  //除霜
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
                    }
                    else
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 1;
                    }
                    break;
                }
                case (1): {
                    *comp1_sig = COMPRESSOR_SIG_ON;
                    *comp2_sig = COMPRESSOR_SIG_ON;

                    if ((get_alarm_bitmap(ACL_E1) || get_alarm_bitmap(ACL_E2)) ||  //水满告警
                        ((comp1_alarm_flag & comp2_alarm_flag) != 0) ||            //告警
                        ((sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST1_STS_BPOS) != 0) &&
                         (sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST2_STS_BPOS) != 0)))  //除霜
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
                    }
                    else if ((comp1_alarm_flag == 1) ||
                             (sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST1_STS_BPOS) != 0))  // alarm alternation
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 1;
                        *comp1_sig                                = COMPRESSOR_SIG_OFF;
                    }
                    else if ((comp2_alarm_flag == 1) ||
                             (sys_get_remap_status(WORK_MODE_STS_REG_NO, DEFROST2_STS_BPOS) != 0))  // alarm alternation
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 1;
                        *comp2_sig                                = COMPRESSOR_SIG_OFF;
                    }
                    else
                    {
                        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 1;
                    }
                    break;
                }
                default: {
                    *comp1_sig                                = COMPRESSOR_SIG_OFF;
                    *comp2_sig                                = COMPRESSOR_SIG_OFF;
                    l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
                    break;
                }
            }
        }
        else
        {
            *comp1_sig                                = COMPRESSOR_SIG_OFF;
            *comp2_sig                                = COMPRESSOR_SIG_OFF;
            l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
        }
    }
    else
    {
        *comp1_sig                                = COMPRESSOR_SIG_OFF;
        *comp2_sig                                = COMPRESSOR_SIG_OFF;
        l_sys.l_fsm_state[COMPRESS_SIG_FSM_STATE] = 0;
    }
    return 1;
}

void compressor_alarm_signal_gen(uint8_t *comp1_sig, uint8_t *comp2_sig)
{
    if ((get_alarm_bitmap_mask(DO_COMP1_BPOS) == 1) && (get_alarm_bitmap_op(DO_COMP1_BPOS) == 0))
    {
        *comp1_sig = COMPRESSOR_SIG_OFF;
        if ((l_sys.l_fsm_state[COMPRESS1_FSM_STATE] != COMPRESSOR_FSM_STATE_STOP) &&
            (l_sys.l_fsm_state[COMPRESS1_FSM_STATE] != COMPRESSOR_FSM_STATE_IDLE))
        {
            l_sys.l_fsm_state[COMPRESS1_FSM_STATE] = COMPRESSOR_FSM_STATE_STOP;
        }
    }

    if ((get_alarm_bitmap_mask(DO_COMP2_BPOS) == 1) && (get_alarm_bitmap_op(DO_COMP2_BPOS) == 0))
    {
        *comp2_sig = COMPRESSOR_SIG_OFF;
        if ((l_sys.l_fsm_state[COMPRESS2_FSM_STATE] != COMPRESSOR_FSM_STATE_STOP) &&
            (l_sys.l_fsm_state[COMPRESS2_FSM_STATE] != COMPRESSOR_FSM_STATE_IDLE))
        {
            l_sys.l_fsm_state[COMPRESS2_FSM_STATE] = COMPRESSOR_FSM_STATE_STOP;
        }
    }
}

//压缩机状态机函数
static void compressor_fsm(uint8_t compressor_id, uint8_t signal)
{
    uint16_t compress_fsm_state;

    uint8_t l_fsm_state_id;
    uint8_t do_bpos;

    if (compressor_id == 0)
    {
        l_fsm_state_id = COMPRESS1_FSM_STATE;
        do_bpos        = DO_COMP1_BPOS;
    }
    else
    {
        l_fsm_state_id = COMPRESS2_FSM_STATE;
        do_bpos        = DO_COMP2_BPOS;
    }

    compress_fsm_state = l_sys.l_fsm_state[l_fsm_state_id];

    //		rt_kprintf("compressor_id=%d,signal=%d,,compress_fsm_state=%d,l_sys.Comp_Close=%d,l_sys.comp_startup_interval=%d\n",compressor_id,signal,compress_fsm_state,l_sys.Comp_Close,l_sys.comp_startup_interval);

    switch (compress_fsm_state)
    {
        case (COMPRESSOR_FSM_STATE_IDLE): {
            if ((signal == COMPRESSOR_SIG_ON) && (l_sys.comp_timeout[do_bpos] == 0))
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_INIT;
                l_sys.comp_timeout[do_bpos]       = g_sys.config.compressor.startup_delay;

                req_bitmap_op(do_bpos, 0);
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_IDLE;
                l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                req_bitmap_op(do_bpos, 0);
            }
            break;
        }
        case (COMPRESSOR_FSM_STATE_INIT): {
            if (signal != COMPRESSOR_SIG_ON)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_IDLE;
                l_sys.comp_timeout[do_bpos]       = 0;
                req_bitmap_op(do_bpos, 0);
            }
            else if ((signal == COMPRESSOR_SIG_ON) && (l_sys.comp_timeout[do_bpos] == 0))
            {
                if (l_sys.comp_startup_interval == 0)
                {
                    l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STARTUP;
                    l_sys.comp_timeout[do_bpos]       = g_sys.config.compressor.startup_lowpress_shield;
                    req_bitmap_op(do_bpos, 1);
                    l_sys.comp_startup_interval = g_sys.config.ComPara.u16Comp_Interval;
                }
                else
                {
                    l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_INIT;
                    l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                    req_bitmap_op(do_bpos, 0);
                }
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_INIT;
                l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                req_bitmap_op(do_bpos, 0);
            }
            break;
        }
        case (COMPRESSOR_FSM_STATE_STARTUP): {
            if (signal == COMPRESSOR_SIG_ERR)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STOP;
            }
            else if (l_sys.comp_timeout[do_bpos] == 0)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_NORMAL;
                l_sys.comp_timeout[do_bpos] =
                    (g_sys.config.compressor.min_runtime > g_sys.config.compressor.startup_lowpress_shield)
                        ? (g_sys.config.compressor.min_runtime - g_sys.config.compressor.startup_lowpress_shield)
                        : 0;
                req_bitmap_op(do_bpos, 1);
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STARTUP;
                l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                req_bitmap_op(do_bpos, 1);
            }
            break;
        }
        case (COMPRESSOR_FSM_STATE_NORMAL): {
            if (signal == COMPRESSOR_SIG_ERR)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STOP;
            }
            else if ((signal == COMPRESSOR_SIG_OFF) && (l_sys.comp_timeout[do_bpos] == 0))
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_SHUTING;
                l_sys.comp_timeout[do_bpos]       = g_sys.config.compressor.stop_delay;
                req_bitmap_op(do_bpos, 1);
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_NORMAL;
                l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                req_bitmap_op(do_bpos, 1);
            }
            break;
        }
        case (COMPRESSOR_FSM_STATE_SHUTING): {
            if (signal == COMPRESSOR_SIG_ERR)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STOP;
            }
            else if ((signal == COMPRESSOR_SIG_OFF) && (l_sys.comp_timeout[do_bpos] == 0))
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STOP;
            }
            else if (signal == COMPRESSOR_SIG_ON)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_NORMAL;
                l_sys.comp_timeout[do_bpos]       = 0;
                req_bitmap_op(do_bpos, 1);
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_SHUTING;
                l_sys.comp_timeout[do_bpos]       = l_sys.comp_timeout[do_bpos];
                req_bitmap_op(do_bpos, 1);
            }
            break;
        }
        case (COMPRESSOR_FSM_STATE_STOP): {
            //						l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_IDLE;
            //						l_sys.comp_timeout[do_bpos] = g_sys.config.compressor.min_stoptime;
            //						req_bitmap_op(do_bpos,0);
            if (l_sys.comp_stop_interval == 0)
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_IDLE;
                //									l_sys.comp_timeout[do_bpos] =
                // g_sys.config.compressor.min_stoptime;//取消最短停机时间
                l_sys.comp_timeout[do_bpos] = g_sys.config.ComPara.u16Start_Delay;  //启动延时
                req_bitmap_op(do_bpos, 0);
                l_sys.comp_stop_interval = g_sys.config.ComPara.u16Comp_Interval;
            }
            else
            {
                l_sys.l_fsm_state[l_fsm_state_id] = COMPRESSOR_FSM_STATE_STOP;
            }
            break;
        }
    }
}

// compressor requirement execution
void compressor_req_exe(int16_t req_temp, int16_t req_hum)
{
    uint8_t comp1_sig, comp2_sig;
    compressor_signal_gen(req_temp, req_hum, &comp1_sig, &comp2_sig);  // FSM signal generation
    compressor_alarm_signal_gen(&comp1_sig, &comp2_sig);               // compressors alarm

    compressor_fsm(0, comp1_sig);  // compressor 1 FSM execution
    compressor_fsm(1, comp2_sig);  // compressor 2 FSM execution
}

#define POWERTIME 5400
#define THINTERVAL 180
//#define CF_DELAY 60
#define CF_DELAY 5 * 2
#define WL_DELAY 30 * 2
enum
{
    FC_WL = 0x01,
    FC_PT = 0x02,
    FC_TH = 0x04,
    FC_WS = 0x08,
    FC_LW = 0x10,
    FC_AL = 0x80,  //告警
};
//外接水源
uint8_t Exit_Water(void)
{
    if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_FLUSH)  //冲洗模式
    {
        return WATER_FLUSH;
    }
    else if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_FILL)  //注水模式
    {
        return WATER_FILL;
    }
    else if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_Disinfection)  //注水模式
    {
        return WATER_Disinfection;
    }
    else
    {
        return WATER_AIR;
    }
}

//外接水源
void External_Water_exe(uint8_t u8Type)
{
    //		uint16_t u16WL;

    if (Exit_Water() != WATER_AIR)  //外接水源
    {
    }

    return;
}

//风机,压机启动条件
void Sys_Fan_CP_WL(void)
{
    uint16_t u16WL;
    uint16_t Test               = 0;
    static uint8_t u8Offset_CNT = 0;
    static uint8_t u8WL_Delay   = 0;
    //    static uint8_t u8FCP_Start = 0;
    //    static uint8_t u8WL_Start = FALSE;

    if (sys_get_pwr_sts() == 1)
    {
        Test = 1;
        l_sys.PowerOn_Time++;
    }
    else
    {
        l_sys.PowerOn_Time = 0;
    }

    u16WL = Get_Water_level();
    // 		rt_kprintf("u16WL=%x,Test=%x,,TH_Check_Interval=%d,din_bitmap[0]=%x\n",u16WL,Test,l_sys.TH_Check_Interval,g_sys.status.ComSta.u16Din_bitmap[0]);
    //		//TEST
    //		u16WL=0x13;
    //满水
    if ((u16WL & D_M) && (u16WL & D_L))
    {
        {
            u8WL_Delay = 0;
            Test |= 0x04;
            l_sys.Fan.Fan_Close[0] |= FC_WL;
            //            l_sys.Comp_Close[0] |= FC_WL;
            //            l_sys.Comp_Close[1] |= FC_WL;
        }
    }
    else
    {
        if ((u16WL & D_M) == 0)  //制水模式
        {
            u8WL_Delay++;
            if (u8WL_Delay >= WL_DELAY)  //满水延迟
            {
                u8WL_Delay = 0;
                Test |= 0x02;
                l_sys.Fan.Fan_Close[0] &= ~FC_WL;
                //            l_sys.Comp_Close[0] &= ~FC_WL;
                //            l_sys.Comp_Close[1] &= ~FC_WL;
            }
        }
    }

    u8Offset_CNT++;
    if (u8Offset_CNT >= CF_DELAY)
    {
        u8Offset_CNT = CF_DELAY;
    }
    if ((l_sys.TH_Check_Interval == 0) && (u8Offset_CNT >= CF_DELAY))  //上电延时判断温湿度
    {
        l_sys.TH_Check_Interval = 0;  //温湿度判断间隔

        {
            //					  u8FCP_Start = 1; //启动
            if (((g_sys.status.ComSta.u16TH[0].Temp >= (int16_t)g_sys.config.ComPara.u16Start_Temp[0]) &&
                 (g_sys.status.ComSta.u16TH[0].Temp < g_sys.config.ComPara.u16Start_Temp[1])) &&
                (g_sys.status.ComSta.u16TH[0].Hum >= g_sys.config.ComPara.u16Start_Humidity))  //温湿度满足条件
            {
                Test |= 0x10;
                l_sys.Fan.Fan_Close[0] &= ~FC_TH;
                //                l_sys.Heat.Heat_Gear = HEATER_SIG_L5;
                //                l_sys.Comp_Close[0] &= ~FC_TH;
                //                l_sys.Comp_Close[1] &= ~FC_TH;
            }
            else if (((g_sys.status.ComSta.u16TH[0].Temp >= (g_sys.config.ComPara.u16Start_Temp[1] + 10)) &&
                      (g_sys.status.ComSta.u16TH[0].Temp < g_sys.config.ComPara.u16Stop_Temp[1])) &&
                     (g_sys.status.ComSta.u16TH[0].Hum >= g_sys.config.ComPara.u16Start_Humidity))  //温湿度满足条件
            {
                Test |= 0x20;
                l_sys.Fan.Fan_Close[0] &= ~FC_TH;
                //                l_sys.Heat.Heat_Gear = HEATER_SIG_L1;
                //                l_sys.Comp_Close[0] |= FC_TH;
                //                l_sys.Comp_Close[1] |= FC_TH;
            }
            else if ((g_sys.status.ComSta.u16TH[0].Temp >= (g_sys.config.ComPara.u16Stop_Temp[1] + 10)) ||
                     (g_sys.status.ComSta.u16TH[0].Hum < g_sys.config.ComPara.u16Start_Humidity) ||
                     (g_sys.status.ComSta.u16TH[0].Temp <
                      ((int16_t)g_sys.config.ComPara.u16Start_Temp[0] - 10)))  //超出条件
            {
                Test |= 0x40;
                l_sys.Fan.Fan_Close[0] |= FC_TH;
                //                l_sys.Heat.Heat_Gear = HEATER_SIG_ZERO;
                //                l_sys.Comp_Close[0] |= FC_TH;
                //                l_sys.Comp_Close[1] |= FC_TH;
            }
            else
            {
            }
        }
    }

    //    if (g_sys.config.ComPara.u16ExitWater_Mode != WATER_AIR) //外接水源
    //    {
    //        Test |= 0x800;
    //        l_sys.Fan_Close |= FC_WS;
    //        l_sys.Comp_Close[0] |= FC_WS;
    //        l_sys.Comp_Close[1] |= FC_WS;
    //    }
    //    else
    //    {
    //        Test |= 0x1000;
    //        l_sys.Fan_Close &= ~FC_WS;
    //        l_sys.Comp_Close[0] &= ~FC_WS;
    //        l_sys.Comp_Close[1] &= ~FC_WS;
    //    }

    if (alarm_Off() == TRUE)  //关机告警
    {
        Test |= 0x100;
        l_sys.Fan.Fan_Close[0] |= FC_AL;
        //        l_sys.Comp_Close[0] |= FC_AL;
        //        l_sys.Comp_Close[1] |= FC_AL;
    }
    else
    {
        Test |= 0x200;
        l_sys.Fan.Fan_Close[0] &= ~FC_AL;
        //        l_sys.Comp_Close[0] &= ~FC_AL;
        //        l_sys.Comp_Close[1] &= ~FC_AL;
    }

    //运行模式
    if (l_sys.Fan.Fan_Close[0])
    {
        Test |= 0x400;
        l_sys.u8Sys_State = FSM_STOP;
    }
    else if ((get_alarm_bitmap(ACL_E24)) || (get_alarm_bitmap(ACL_E25)))
    {
        Test |= 0x800;
        l_sys.u8Sys_State = FSM_IDLE;
    }
    else
    {
        Test |= 0x1000;
        l_sys.u8Sys_State = FSM_RUN;
    }
    g_sys.status.ComSta.REQ_TEST[3] = Test;
    g_sys.status.ComSta.REQ_TEST[4] = l_sys.Fan.Fan_Close[0];

    rt_kprintf("REQ_TEST[3]=%04x,REQ_TEST[4]=%04x,status_remap=%04x,u16WL=%04x\n", g_sys.status.ComSta.REQ_TEST[3],
               g_sys.status.ComSta.REQ_TEST[4], g_sys.status.status_remap[0], g_sys.status.ComSta.u16WL);

    return;
}

//原水箱水位状态
uint16_t Get_Water_Source(void)
{
    uint16_t u16WL;
    uint16_t u16WL_S;
    u16WL = Get_Water_level();  //水位
    if (u16WL & 0x01)           //低水位
    {
        u16WL_S = S_L;
    }
    else if (u16WL & 0x02)  //高水位
    {
        u16WL_S = S_M;
    }
    else
    {
        u16WL_S = 0;
    }
    return u16WL_S;
}

//净化泵
void Collect_exe(void)
{
    uint16_t u16WL;
    uint16_t u16WL_S;
    static uint8_t u8PWP_S        = FALSE;
    static uint8_t u8CollectDelay = 0;
    uint16_t u16TEST;

    u16TEST = 0;
    if (l_sys.u8Storage_Status == TRUE)
    {
        return;
    }
    u16WL                           = Get_Water_level();   //水位
    u16WL_S                         = Get_Water_Source();  //原水位状态
    g_sys.status.ComSta.REQ_TEST[2] = u16WL_S;
    if (l_sys.Pwp_Open == TRUE)  //需要收集
    {
        u16TEST |= 0x01;
    }
    else
    {
        if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_FLUSH)  //冲洗模式
        {
            u16TEST |= 0x02;
            if (u16WL & D_M)  //饮水箱高水位,自动退出
            {
                req_bitmap_op(DO_P1_BPOS, 0);   //泵1
                req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
            }
            else
            {
                u16TEST |= 0x04;
                if (get_alarm_bitmap(ACL_E17))  //缺水故障
                {
                    req_bitmap_op(DO_P1_BPOS, 0);   //泵1
                    req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                }
                else
                {
                    req_bitmap_op(DO_P1_BPOS, 1);   //泵1
                    req_bitmap_op(DO_EV2_BPOS, 1);  //出水阀
                }
            }
            //        rt_kprintf("u16WL=%x,u16TEST=%x,u16ExitWater_Mode=%d\n",u16WL,u16TEST,g_sys.config.ComPara.u16ExitWater_Mode);
            return;
        }
        else if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_FILL)  //注水模式
        {
            req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
            //源水箱中水位
            //            if ((u16WL & S_M) || (u16WL & S_U))
            if (u16WL_S == S_M)
            {
                u16TEST |= 0x08;
                l_sys.Pwp_Open = TRUE;
                req_bitmap_op(DO_P1_BPOS, 0);  //泵1
                return;                        //需要收集
            }
            if (u16WL & D_M)  //饮水箱高水位,关闭
            {
                u16TEST |= 0x10;
                l_sys.u8ExitFill = TRUE;
                req_bitmap_op(DO_P1_BPOS, 0);  //泵1
            }
            else
            {
                if (get_alarm_bitmap(ACL_E17))  //缺水故障
                {
                    u16TEST |= 0x20;
                    req_bitmap_op(DO_P1_BPOS, 0);  //泵1
                }
                else
                {
                    if ((l_sys.u8ExitFill == FALSE) || ((u16WL & D_ML) == 0))
                    {
                        l_sys.u8ExitFill = FALSE;      //注水中
                        req_bitmap_op(DO_P1_BPOS, 1);  //泵1
                    }
                }
            }
            //        rt_kprintf("u16WL=%x,u16TEST=%x,u16ExitWater_Mode=%d\n",u16WL,u16TEST,g_sys.config.ComPara.u16ExitWater_Mode);
            return;
        }
        else if (g_sys.config.ComPara.u16ExitWater_Mode == WATER_Disinfection)  //消毒模式
        {
            req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀 //循环阀
            if (u16WL & D_M)                //饮水箱高水位,自动退出
            {
                u16TEST |= 0x40;
                req_bitmap_op(DO_P1_BPOS, 0);  //泵1
                g_sys.config.ComPara.u16ExitWater_Mode = 0;
                RAM_Write_Reg(EE_EXITWATER, g_sys.config.ComPara.u16ExitWater_Mode, 1);
                //								g_sys.config.ComPara.u16Disinfection_Mode=0;
                //								RAM_Write_Reg(EE_DISINFECTION,g_sys.config.ComPara.u16Disinfection_Mode,1);
            }
            else
            {
                req_bitmap_op(DO_P1_BPOS, 1);  //泵2
            }
            return;
        }
        else
        {
            u16TEST |= 0x80;
            req_bitmap_op(DO_P1_BPOS, 0);   //泵1
            req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                                            //        g_sys.config.ComPara.u16Disinfection_Mode = 0;
        }
    }
    //        rt_kprintf("u16WL=%x,u16TEST=%x\n",u16WL,u16TEST);

    //源水箱中水位
    //    if (u16WL & S_M)
    if (u16WL_S == S_M)
    {
        u16TEST |= 0x100;
        l_sys.Pwp_Open = TRUE;
    }
    //外壳打开时，开净化泵
    else if ((get_alarm_bitmap(ACL_E4)) && (g_sys.config.ComPara.u16Water_Ctrl & OPEN_PWP))
    {
        l_sys.Pwp_Open = TRUE;
    }
    else
    {
        l_sys.Pwp_Open = FALSE;
    }
    //复合滤芯堵塞,强制关闭
    if ((get_alarm_bitmap(ACL_E9)) || (get_alarm_bitmap(ACL_E28)) || (get_alarm_bitmap(ACL_E29)))
    {
        l_sys.Pwp_Open = FALSE;
        u8PWP_S        = FALSE;
    }
    //饮水箱高水位，暂停
    if (u16WL & D_M)
    {
        l_sys.Pwp_Open = FALSE;
        u8PWP_S        = FALSE;
    }

    //开净化泵
    if ((l_sys.Pwp_Open == TRUE) || (u8PWP_S == TRUE))
    {
        u16TEST |= 0x200;
        req_bitmap_op(DO_P1_BPOS, 1);
        req_bitmap_op(DO_EV3_BPOS, 1);  //进水阀
        req_bitmap_op(DO_EV4_BPOS, 1);  //缓冲阀
        u8PWP_S = TRUE;
        u8CollectDelay++;
        if (u8CollectDelay >= g_sys.config.ComPara.u16CollectGasDelay * OFFSET_DELAY)
        {
            u8CollectDelay = g_sys.config.ComPara.u16CollectGasDelay * OFFSET_DELAY;
            req_bitmap_op(DO_EV4_BPOS, 0);  //缓冲阀
        }
        //        if (((u16WL & S_M) == 0) && ((u16WL & S_L) == 0))  //到低水位退出
        if (u16WL_S == S_L)
        {
            u8PWP_S = FALSE;
            req_bitmap_op(DO_P1_BPOS, 0);
            req_bitmap_op(DO_EV3_BPOS, 0);  //进水阀
            req_bitmap_op(DO_EV4_BPOS, 0);  //缓冲阀
            u8CollectDelay = 0;
        }
        l_sys.Pwp_Open = u8PWP_S;
    }
    else
    {
        u16TEST |= 0x400;
        req_bitmap_op(DO_P1_BPOS, 0);
        req_bitmap_op(DO_EV3_BPOS, 0);  //进水阀
        req_bitmap_op(DO_EV4_BPOS, 0);  //缓冲阀
        u8CollectDelay = 0;
    }

    return;
}
//流量计算
uint16_t PluseCalc_Water(uint16_t PluseCnt)
{
    uint16_t u16Water_Flow;
    float fWflow;

    //		if(PluseCnt<L200)//小于0.15L
    //		{
    //			fWflow=	(float)PluseCnt*L200_FACTOR;
    //		}
    //		else
    if (PluseCnt < L300)  //小于0.3L
    {
        fWflow = (float)PluseCnt * L300_FACTOR;
    }
    else if (PluseCnt < L500)
    {
        fWflow = (float)PluseCnt * L500_FACTOR;
    }
    else if (PluseCnt < L1000)
    {
        fWflow = (float)PluseCnt * L1000_FACTOR;
    }
    else if (PluseCnt < L1500)
    {
        fWflow = (float)PluseCnt * L1500_FACTOR;
    }
    else if (PluseCnt < L2000)
    {
        fWflow = (float)PluseCnt * L2000_FACTOR;
    }
    else
    {
        fWflow = (float)PluseCnt * L2000_FACTOR;
    }

    u16Water_Flow = (uint16_t)fWflow;

    return u16Water_Flow;
}
//按键出水检测
void WaterOut_Key(void)
{
    //
    //

    //    //冷水 1
    //    if ((sys_get_di_sts(DI_Cold_1_BPOS) == 1))
    //    {
    //        l_sys.OutWater_Key |= WATER_NORMAL_ICE;
    //        l_sys.OutWater_Delay[0] = WATER_MAXTIME;
    //    }
    //    else
    //    {

    //        l_sys.OutWater_Key &= ~WATER_NORMAL_ICE;
    //        l_sys.OutWater_Delay[0] = 0;
    //    }

    //    //冷水 2
    //    if ((sys_get_di_sts(DI_Cold_2_BPOS) == 1) && (g_sys.config.ComPara.u16Water_Ctrl & TWO_COLD)) //双路出冷水
    //    {
    //        l_sys.OutWater_Key |= WATER_ICE;
    //        l_sys.OutWater_Delay[2] = WATER_MAXTIME;
    //    }
    //    else
    //    {
    //        l_sys.OutWater_Key &= ~WATER_ICE;
    //        l_sys.OutWater_Delay[2] = 0;
    //    }

    //    if (l_sys.ChildLock_Cnt[0] >= ChildKey_Cnt)
    //    {
    //        l_sys.ChildLock_Cnt[0] = 0;
    //        l_sys.ChildLock_Key = 1;
    //        l_sys.ChildLock_Cnt[1] = ChildKey_Lose;
    //    }
    //    //童锁使能
    //    if (l_sys.ChildLock_Key)
    //    {
    //        //热水
    //        if ((sys_get_di_sts(DI_Heat_BPOS) == 1))
    //        {
    //            l_sys.OutWater_Key |= WATER_HEAT;
    //            l_sys.OutWater_Delay[1] = WATER_MAXTIME;
    //        }
    //        else
    //        {
    //            l_sys.OutWater_Key &= ~WATER_HEAT;
    //            l_sys.OutWater_Delay[1] = 0;
    //        }
    //    }
    //    else
    //    {
    //        if ((sys_get_di_sts(DI_Heat_BPOS) == 1)) //无效
    //        {
    //        }
    //        else
    //        {
    //            l_sys.OutWater_Key &= ~WATER_HEAT;
    //            l_sys.OutWater_Delay[1] = 0;
    //        }
    //    }

    return;
}
//关闭出水
uint8_t WaterOut_Close(uint8_t u8Type, uint8_t u8Water)
{
    uint8_t u8CloseWP = FALSE;
    static uint8_t u8CloseNum;

    if (u8Type == 0)
    {
        u8CloseNum = 0;
    }
    else if (u8Type == 1)
    {
        l_sys.LedKey.OutWater           = FALSE;
        l_sys.comp_timeout[DO_EV2_BPOS] = 0;         //出水计时
        l_sys.OutWater_Flag             = WATER_NO;  //关闭出水
                                                     //        //外接水源
                                                     //        External_Water_exe(FALSE);
        if (l_sys.OutWater_OK)
        {
            l_sys.OutWater_OK = WATER_OUT;
            if (g_sys.status.ComSta.u16Cur_Water)
            {
                //                l_sys.u32WaterFlow += g_sys.status.ComSta.u16Cur_Water; //+当次出水流量
                //                g_sys.status.ComSta.u16Cumulative_Water[0] = l_sys.u32WaterFlow;
                //                g_sys.status.ComSta.u16Cumulative_Water[1] = l_sys.u32WaterFlow >> 16;
                g_sys.status.ComSta.u16Last_Water = g_sys.status.ComSta.u16Cur_Water;

                g_sys.status.ComSta.u16Cur_Water   = 0;
                g_sys.config.ComPara.u16Water_Mode = 0;
                g_sys.config.ComPara.u16Water_Flow = 0;
            }
            if (u8Water == WATER_NORMAL_ICE)
            {
                //常温水相关
                req_bitmap_op(DO_EV1_BPOS, 0);  //循环阀
                if (g_sys.config.ComPara.u16CloseDelay)
                {
                    l_sys.u8CloseDelay = g_sys.config.ComPara.u16CloseDelay;
                    if (g_sys.config.ComPara.u16CloseFrist == PUMP_FIRET)
                    {
                        req_bitmap_op(DO_P1_BPOS, 0);  //出水泵
                        u8CloseWP = TRUE;
                    }
                    else
                    {
                        req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                    }
                }
                else
                {
                    req_bitmap_op(DO_P1_BPOS, 0);  //出水泵
                    u8CloseWP = TRUE;
                    req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                }
            }
            else if (u8Water == WATER_HEAT)
            {
            }
            else if (u8Water == WATER_ICE)
            {
            }
        }
        else
        {
            if (l_sys.u8CloseDelay)
            {
                l_sys.u8CloseDelay--;
                if ((g_sys.config.ComPara.u16CloseDelay) && (l_sys.u8CloseDelay == 0))
                {
                    l_sys.u8CloseDelay = 0;
                    if (g_sys.config.ComPara.u16CloseFrist == PUMP_FIRET)
                    {
                        req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                    }
                    else
                    {
                        req_bitmap_op(DO_P1_BPOS, 0);  //出水泵
                        u8CloseWP = TRUE;
                    }
                }
            }
        }

        //关闭出水泵
        if ((u8CloseWP == TRUE) && (l_sys.Sterilize == FALSE))  //未循环
        {
            req_bitmap_op(DO_P1_BPOS, 0);  //出水泵
        }

        if (u8Water == WATER_HEAT)
        {
            l_sys.HeatWater_Time = 0;
            l_sys.HeatWater_Flow = 0;
            //即热式出水器
            if (u8CloseNum < CLOSEHEAT_MAX)  //关闭出水
            {
                g_sys.status.ComSta.REQ_TEST[1] |= 0x1000;
                u8CloseNum++;
                if (Heat_Send(HEAT_WRITEPARA, CLOSE_HEAT, g_sys.config.ComPara.u16Water_Flow))
                {
                }
            }
        }
    }
    else if (u8Type == 2)
    {
        if (u8Water == WATER_HEAT)  //第一次上电，需要出100ml水后，临时关闭一下热水，然后继续出水
        {
            //            l_sys.HeatWater_Time = 0;
            //            l_sys.HeatWater_Flow = 0;
            //即热式出水器
            if (u8CloseNum < CLOSEHEAT_MAX)  //关闭出水
            {
                g_sys.status.ComSta.REQ_TEST[1] |= 0x2000;
                u8CloseNum++;
                if (Heat_Send(HEAT_WRITEPARA, CLOSE_HEAT, g_sys.config.ComPara.u16Water_Flow))
                {
                }
            }
            else
            {
                return TRUE;
            }
        }
    }

    return FALSE;
}
//饮水箱低水位判断
uint8_t WaterOut_level(void)
{
    uint16_t u16WL;

    //水位
    u16WL = Get_Water_level();
    //    if (Exit_Water() != WATER_AIR) //外接水源
    //    {
    //        return TRUE;
    //    }
    //    else
    {
        if (!(u16WL & D_L))
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

//出水
void WaterOut_req_exe(void)
{
    //		uint32_t u32CW;
    uint16_t u16Pls_Cnt;
    static uint8_t u8HeatNum;
    static uint8_t u8HeatFirst = 0;

    g_sys.status.ComSta.REQ_TEST[1] = 0;
    //    req_bitmap_op(DO_RH1_BPOS, 1);  //电加热
    if (l_sys.u8Storage_Status == TRUE)
    {
        return;
    }

    //					rt_kprintf("HeatWater_st=%d,HeatWater_Flow=%d,OutWater_OK=%d,u8HeatNum=%d,u16Cur_Water=%d\n",l_sys.HeatWater_st,l_sys.HeatWater_Flow,l_sys.OutWater_OK,u8HeatNum,g_sys.status.ComSta.u16Cur_Water);
    //常温水优化
    if (((g_sys.config.ComPara.u16HotWater_Temp <= 25) && (g_sys.config.ComPara.u16Heater_PM25 == Heater_JB)) ||
        ((g_sys.config.ComPara.u16HotWater_Temp <= 0x01) && (g_sys.config.ComPara.u16Heater_PM25 == Heater_JX)))
    {
        if (g_sys.config.ComPara.u16Water_Mode == WATER_HEAT)
        {
            g_sys.config.ComPara.u16Water_Mode = WATER_NORMAL_ICE;  //常温水
        }
    }

    if (g_sys.config.ComPara.u16Water_Ctrl & HMI_KEY)  //按键出水
    {
        g_sys.status.ComSta.REQ_TEST[1] |= 0x01;
        WaterOut_Key();
        if (WaterOut_level() == FALSE)
            l_sys.LedKey.OutWater = FALSE;
        if ((!(l_sys.OutWater_Key)) || (WaterOut_level() == FALSE))  //饮水箱低水位
        {
            g_sys.status.ComSta.REQ_TEST[1] |= 0x02;
            u8HeatNum            = 0;
            l_sys.HeatWater_st   = HEAT_NO;
            l_sys.HeatWater_Flow = 0;
            WaterOut_Close(1, WATER_NORMAL_ICE);
            WaterOut_Close(1, WATER_HEAT);
            WaterOut_Close(1, WATER_ICE);

            return;
        }
    }
    else  // HMI出水
    {
        if ((!(g_sys.config.ComPara.u16Water_Mode) && !(g_sys.config.ComPara.u16Water_Flow)) ||
            (WaterOut_level() == FALSE))  //饮水箱低水位,不允许出水
        {
            g_sys.status.ComSta.REQ_TEST[1] |= 0x04;
            if (WaterOut_level() == FALSE)  //饮水箱低水位
            {
                g_sys.status.ComSta.REQ_TEST[1] |= 0x08;
                g_sys.config.ComPara.u16Water_Mode = 0;
                g_sys.config.ComPara.u16Water_Flow = 0;
            }
            u8HeatNum            = 0;
            l_sys.HeatWater_st   = HEAT_NO;
            l_sys.HeatWater_Flow = 0;
            WaterOut_Close(1, WATER_NORMAL_ICE);
            WaterOut_Close(1, WATER_HEAT);
            WaterOut_Close(1, WATER_ICE);
            return;
        }
    }

    //门打开,关闭出水
    if ((get_alarm_bitmap(ACL_E4)) || (get_alarm_bitmap(ACL_E28)))
    {
        g_sys.status.ComSta.REQ_TEST[1] |= 0x4000;
        WaterOut_Close(1, WATER_NORMAL_ICE);
        WaterOut_Close(1, WATER_HEAT);
        WaterOut_Close(1, WATER_ICE);
        return;
    }
    //		rt_kprintf("HeatWater_st=%d,HeatWater_Flow=%d,OutWater_OK=%d,u8HeatNum=%d,u16Cur_Water=%d,OutWater_Key=%x\n",l_sys.HeatWater_st,l_sys.HeatWater_Flow,l_sys.OutWater_OK,u8HeatNum,g_sys.status.ComSta.u16Cur_Water,l_sys.OutWater_Key);
    if (((g_sys.config.ComPara.u16Water_Mode == WATER_HEAT) && (g_sys.config.ComPara.u16Water_Flow)) ||
        (l_sys.OutWater_Key & WATER_HEAT))  //热水
    {
        g_sys.status.ComSta.REQ_TEST[1] |= 0x10;
        l_sys.comp_timeout[DO_EV2_BPOS]++;  //出水计时

        if (!(g_sys.config.ComPara.u16Water_Ctrl & HEART_POT))  //即热式出水
        {
            //					rt_kprintf("HeatWater_Time=%d,OutWater_Flag=%d,g_sys.status.ComSta.u16Cur_Water=%x\n",l_sys.HeatWater_Time,l_sys.OutWater_Flag,g_sys.status.ComSta.u16Cur_Water);
            if (g_sys.config.ComPara.u16Heater_PM25 == Heater_JX)  //佳芯
            {
                WaterOut_Close(0, WATER_HEAT);
                g_sys.status.ComSta.REQ_TEST[1] |= 0x20;
                if ((l_sys.OutWater_OK == WATER_READ) || ((l_sys.OutWater_OK == HEATER_SEND) && (u8HeatNum >= 3)))
                {
                    g_sys.status.ComSta.REQ_TEST[1] |= 0x40;

                    //                if (l_sys.HeatWater_st == HEAT_OUTWATER) //开始出水
                    //                {

                    //                    l_sys.OutWater_Flag = WATER_HEAT; //出水中
                    //                }
                    //                else //时间计算,500ml/MIN,
                    {
                        g_sys.status.ComSta.REQ_TEST[1] |= 0x100;
                        l_sys.HeatWater_Time += 1;      // 500ms执行一次
                        if (l_sys.HeatWater_Time >= 2)  //延时2秒
                        {
                            WaterOut_Close(0, WATER_HEAT);

                            l_sys.HeatWater_Flow             = (l_sys.HeatWater_Time - 2) * HEAT_FACTOR_500MS;
                            g_sys.status.ComSta.u16Cur_Water = l_sys.HeatWater_Flow;
                            if (g_sys.status.ComSta.u16Cur_Water)
                            {
                                g_sys.status.ComSta.u16Last_Water = g_sys.status.ComSta.u16Cur_Water;
                            }
                            l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                        }
                    }
                    //								rt_kprintf("HeatWater_Time=%d,OutWater_Flag=%d,g_sys.status.ComSta.u16Cur_Water=%x\n",l_sys.HeatWater_Time,l_sys.OutWater_Flag,g_sys.status.ComSta.u16Cur_Water);
                }

                if ((l_sys.OutWater_OK == HEATER_SEND) || (l_sys.OutWater_OK == WATER_READ))
                {
                    if ((!l_sys.comp_timeout[DO_RH1_BPOS]) && (!g_sys.status.ComSta.u16Cur_Water))
                    {
                        //出水故障
                    }
                }

                //				rt_kprintf("HeatWater_st=%d,HeatWater_Flow=%d,OutWater_OK=%d,u8HeatNum=%d,u16Cur_Water=%d\n",l_sys.HeatWater_st,l_sys.HeatWater_Flow,l_sys.OutWater_OK,u8HeatNum,g_sys.status.ComSta.u16Cur_Water);
                // HMI出水时才判断流量
                if (((g_sys.status.ComSta.u16Cur_Water >= g_sys.config.ComPara.u16Water_Flow) &&
                     (g_sys.status.ComSta.u16Cur_Water)) &&
                    (!(g_sys.config.ComPara.u16Water_Ctrl & HMI_KEY)))  // HMI出水
                {
                    WaterOut_Close(1, WATER_HEAT);
                }
                else
                {
                    if ((u8HeatFirst < 3) && (g_sys.status.ComSta.u16Cur_Water >= 45))  //第一次上电，先出约100ML
                    {
                        WaterOut_Close(2, WATER_HEAT);
                        u8HeatFirst++;
                        //										if(WaterOut_Close(2, WATER_HEAT)==TRUE)
                        //										{
                        //												rt_thread_delay(3000);	//延时3秒
                        //												WaterOut_Close(0, WATER_HEAT);
                        //												u8HeatFirst=1;
                        //												return;
                        //										}
                        return;
                    }

                    g_sys.status.ComSta.REQ_TEST[1] |= 0x200;
                    if (((g_sys.config.ComPara.u16Water_Mode) && (g_sys.config.ComPara.u16Water_Flow)) ||
                        (l_sys.OutWater_Key & WATER_HEAT))
                    {
                        l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                        //串口通信
                        //                    if ((l_sys.OutWater_OK == HEATER_SEND) && (u8HeatNum >= 3))
                        //                    {
                        //                        g_sys.status.ComSta.REQ_TEST[1] |= 0x400;
                        //                        l_sys.OutWater_OK = WATER_READ;
                        //                    }
                        //                    else
                        {
                            if (u8HeatNum < (WRITEHEAT_MAX))  //
                            {
                                g_sys.status.ComSta.REQ_TEST[1] |= 0x800;
                                u8HeatNum++;
                                l_sys.OutWater_OK  = HEATER_SEND;
                                l_sys.HeatWater_st = HEAT_NO;

                                //                            if (l_sys.OutWater_Key & WATER_HEAT) //按键热水,流量不限制
                                if (g_sys.config.ComPara.u16Water_Flow >= 1000)
                                {
                                    g_sys.config.ComPara.u16Water_Flow = 1000;
                                }
                                //		rt_kprintf("u8HeatNum=%d,OutWater_OK=%d,u16Water_Mode=%d,REQ_TEST[1]=%x,u16HotWater_Temp=%d\n",u8HeatNum,l_sys.OutWater_OK,g_sys.config.ComPara.u16Water_Mode,g_sys.status.ComSta.REQ_TEST[1],g_sys.config.ComPara.u16HotWater_Temp);
                                if (Heat_Send(HEAT_WRITEPARA, OPEN_HEAT, g_sys.config.ComPara.u16Water_Flow))
                                {
                                    //提前显示出水中
                                    l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                                }
                                l_sys.comp_timeout[DO_RH1_BPOS] = RH_DEALY;
                            }
                            else
                            {
                            }
                        }
                    }
                }
            }
            else if (g_sys.config.ComPara.u16Heater_PM25 == Heater_JB)  //吉宝
            {
                WaterOut_Close(0, WATER_HEAT);
                g_sys.status.ComSta.REQ_TEST[1] |= 0x20;
                //            if (l_sys.OutWater_OK == WATER_READ)
                //            {
                //                g_sys.status.ComSta.REQ_TEST[1] |= 0x40;

                if (l_sys.HeatWater_st == HEAT_OUTWATER)  //开始出水
                {
                    g_sys.status.ComSta.REQ_TEST[1] |= 0x80;
                    //									WaterOut_Close(0,WATER_HEAT);

                    g_sys.status.ComSta.u16Cur_Water = l_sys.HeatWater_Flow;
                    if (g_sys.status.ComSta.u16Cur_Water)
                    {
                        g_sys.status.ComSta.u16Last_Water = g_sys.status.ComSta.u16Cur_Water;
                    }
                    l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                }
                //            }

                if ((l_sys.OutWater_OK == HEATER_SEND) || (l_sys.OutWater_OK == WATER_READ))
                {
                    if ((!l_sys.comp_timeout[DO_RH1_BPOS]) && (!g_sys.status.ComSta.u16Cur_Water))
                    {
                        //出水故障
                    }
                }

                //				rt_kprintf("HeatWater_st=%d,HeatWater_Flow=%d,OutWater_OK=%d,u8HeatNum=%d,u16Cur_Water=%d\n",l_sys.HeatWater_st,l_sys.HeatWater_Flow,l_sys.OutWater_OK,u8HeatNum,g_sys.status.ComSta.u16Cur_Water);
                // HMI出水时才判断流量
                if ((((g_sys.status.ComSta.u16Cur_Water >= (g_sys.config.ComPara.u16Water_Flow - 10)) ||
                      ((g_sys.status.ComSta.u16Cur_Water >= HEATFLOW_MAX))) &&
                     (g_sys.status.ComSta.u16Cur_Water)) &&
                    (!(g_sys.config.ComPara.u16Water_Ctrl & HMI_KEY)))  // HMI出水
                {
                    WaterOut_Close(1, WATER_HEAT);
                }
                else
                {
                    g_sys.status.ComSta.REQ_TEST[1] |= 0x200;
                    if (((g_sys.config.ComPara.u16Water_Mode) && (g_sys.config.ComPara.u16Water_Flow)) ||
                        (l_sys.OutWater_Key & WATER_HEAT))
                    {
                        l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                        //                        rt_kprintf("u8HeatNum=%d,OutWater_OK=%d,.HeatWater_Flow=%d,.HeatWater_st=%d,u8HeatFirst=%d\n",
                        //                                   u8HeatNum, l_sys.OutWater_OK, l_sys.HeatWater_Flow,
                        //                                   l_sys.HeatWater_st, u8HeatFirst);
                        //串口通信

                        if (l_sys.HeatWater_CMD == FALSE)
                        {
                            g_sys.status.ComSta.REQ_TEST[1] |= 0x800;
                            u8HeatNum++;
                            l_sys.OutWater_OK = HEATER_SEND;

                            //                            if (l_sys.OutWater_Key & WATER_HEAT) //按键热水,流量不限制
                            //											if(g_sys.config.ComPara.u16Water_Flow >=
                            // HEATFLOW_MAX)
                            //											{
                            //													g_sys.config.ComPara.u16Water_Flow =
                            // HEATFLOW_MAX;
                            //											}
                            //															rt_kprintf("u8HeatNum=%d,OutWater_OK=%d,u16Water_Mode=%d,REQ_TEST[1]=%x\n",u8HeatNum,l_sys.OutWater_OK,g_sys.config.ComPara.u16Water_Mode,g_sys.status.ComSta.REQ_TEST[1]);
                            if (Heat_Send(HEAT_WRITEPARA, OPEN_HEAT, g_sys.config.ComPara.u16Water_Flow))
                            {
                                //提前显示出水中
                                l_sys.OutWater_Flag = WATER_HEAT;  //出水中
                            }
                            l_sys.comp_timeout[DO_RH1_BPOS] = RH_DEALY;
                            l_sys.HeatWater_CMD             = TRUE;
                        }
                        else
                        {
                            g_sys.status.ComSta.REQ_TEST[1] |= 0x400;
                            l_sys.OutWater_OK = WATER_READ;

                            if (Heat_Send(HEAT_READPARA, 0, 0))
                            {
                                g_ComStat[UART_HEAT]     = SEND_Over;  //发送完成
                                g_sys.status.ComSta.TEST = 0;
                            }
                            l_sys.HeatWater_CMD = FALSE;
                        }
                    }
                    //								rt_kprintf("u8HeatNum=%d,OutWater_OK=%d,u16Water_Mode=%d,REQ_TEST[1]=%x\n",u8HeatNum,l_sys.OutWater_OK,g_sys.config.ComPara.u16Water_Mode,g_sys.status.ComSta.REQ_TEST[1]);
                }
            }
            else
            {
                WaterOut_Close(0, WATER_HEAT);
            }
        }
        else  //热灌
        {
            u16Pls_Cnt = Read_Pluse_Cnt();
            //流量计算
            if (l_sys.OutWater_OK != WATER_READ)
            {
                Clear_Pluse_Cnt(&u16Pls_Cnt);  //清计数器
            }
            g_sys.status.ComSta.u16Pluse_CNT = u16Pls_Cnt;
            g_sys.status.ComSta.u16Cur_Water = PluseCalc_Water(u16Pls_Cnt);
            if (g_sys.status.ComSta.u16Cur_Water)
            {
                g_sys.status.ComSta.u16Last_Water = g_sys.status.ComSta.u16Cur_Water;
            }
            if (l_sys.OutWater_OK == WATER_READ)
            {
                if ((!l_sys.comp_timeout[DO_RH1_BPOS]) && (!g_sys.status.ComSta.u16Cur_Water))
                {
                    //出水故障
                }
                //								WaterOut_Close(0,WATER_HEAT);
            }
            //				rt_kprintf("u16Pluse_CNT=%d,u16Cur_Water=%d,u16Pls_Cnt=%d\n",g_sys.status.ComSta.u16Pluse_CNT,g_sys.status.ComSta.u16Cur_Water,u16Pls_Cnt);
            if ((g_sys.config.ComPara.u16Water_Mode == WATER_HEAT) || (l_sys.OutWater_Key & WATER_HEAT))  //热水
            {
                //                g_sys.status.ComSta.REQ_TEST[1] |= 0x1000;
                // HMI出水时才判断流量
                if (((g_sys.status.ComSta.u16Cur_Water >= g_sys.config.ComPara.u16Water_Flow) &&
                     (g_sys.status.ComSta.u16Cur_Water)) &&
                    (!(g_sys.config.ComPara.u16Water_Ctrl & HMI_KEY)))  // HMI出水
                {
                    WaterOut_Close(1, WATER_HEAT);
                }
                else
                {
                    //                    g_sys.status.ComSta.REQ_TEST[1] |= 0x2000;
                    l_sys.OutWater_OK = WATER_READ;
                    //                    req_bitmap_op(DO_HWP_BPOS, 1); //热水出水泵
                    //										req_bitmap_op(DO_EV2_BPOS,1);//出水阀
                    l_sys.comp_timeout[DO_RH1_BPOS] = RH_DEALY;
                    l_sys.OutWater_Flag             = WATER_HEATPOT;  //出水中
                }
            }
        }
    }
    else if ((((g_sys.config.ComPara.u16Water_Mode == WATER_NORMAL_ICE) ||
               (g_sys.config.ComPara.u16Water_Mode == WATER_ICE)) &&
              (g_sys.config.ComPara.u16Water_Flow)) ||
             (l_sys.OutWater_Key & WATER_NORMAL_ICE) || (l_sys.OutWater_Key & WATER_ICE))  //常温水/冰水
    {
        l_sys.comp_timeout[DO_EV2_BPOS]++;  //出水计时

        u16Pls_Cnt = Read_Pluse_Cnt();
        //		    rt_kprintf("OutWater_OK=%d,u16Pls_Cnt=%d\n",l_sys.OutWater_OK,u16Pls_Cnt);
        //流量计算
        if (l_sys.OutWater_OK != WATER_READ)
        {
            Clear_Pluse_Cnt(&u16Pls_Cnt);  //清计数器
        }
        g_sys.status.ComSta.u16Pluse_CNT = u16Pls_Cnt;
        g_sys.status.ComSta.u16Cur_Water = PluseCalc_Water(u16Pls_Cnt);
        if (g_sys.status.ComSta.u16Cur_Water)
        {
            g_sys.status.ComSta.u16Last_Water = g_sys.status.ComSta.u16Cur_Water;
        }
        //		    rt_kprintf("u16Last_Water=%d,u16Cur_Water=%d,OutWater_OK==%d,u16Pls_Cnt=%d\n",g_sys.status.ComSta.u16Last_Water,g_sys.status.ComSta.u16Cur_Water,l_sys.OutWater_OK,g_sys.status.ComSta.u16Pluse_CNT);
        if (l_sys.OutWater_OK == WATER_READ)
        {
            if ((!l_sys.comp_timeout[DO_RH1_BPOS]) && (!g_sys.status.ComSta.u16Cur_Water))
            {
                //出水故障
            }
        }
        // HMI出水时才判断流量
        if (((g_sys.status.ComSta.u16Cur_Water >= g_sys.config.ComPara.u16Water_Flow) &&
             (g_sys.status.ComSta.u16Cur_Water)) &&
            (!(g_sys.config.ComPara.u16Water_Ctrl & HMI_KEY)))  // HMI出水
        {
            WaterOut_Close(1, WATER_NORMAL_ICE);
        }
        else
        {
            g_sys.status.ComSta.REQ_TEST[1] |= 0x8000;
            //出水1
            //外接水源
            External_Water_exe(TRUE);

            //            if (Exit_Water() != WATER_AIR) //外接水源,直接出水
            //            {
            //                req_bitmap_op(DO_EV3_BPOS, 1); //外接阀
            //            }
            //            else
            //            {
            //                req_bitmap_op(DO_EV3_BPOS, 0); //
            //            }
            req_bitmap_op(DO_P1_BPOS, 1);   //出水泵
            req_bitmap_op(DO_EV1_BPOS, 1);  //循环阀

            req_bitmap_op(DO_EV2_BPOS, 1);  //出水阀

            l_sys.OutWater_OK               = WATER_READ;
            l_sys.comp_timeout[DO_RH1_BPOS] = RH_DEALY;
            l_sys.OutWater_Flag             = WATER_NORMAL_ICE;  //出水中
        }
    }
    else  //关闭出水
    {
        WaterOut_Close(1, WATER_NORMAL_ICE);
        WaterOut_Close(1, WATER_HEAT);
        WaterOut_Close(1, WATER_ICE);
    }
    //最大出水限制
    if (l_sys.comp_timeout[DO_EV2_BPOS] >= WATER_MAXTIME)
    {
        WaterOut_Close(1, WATER_NORMAL_ICE);
        WaterOut_Close(1, WATER_HEAT);
        WaterOut_Close(1, WATER_ICE);
    }
    return;
}

// UV开关
void UV_req_exe(uint8_t u8Type)
{
    if (u8Type == TRUE)
    {
        if (g_sys.config.ComPara.u16Sterilize_Mode == 1)  //
        {
            req_bitmap_op(DO_UV1_BPOS, 1);  //紫外灯常开
        }
        else if (g_sys.config.ComPara.u16Sterilize_Mode == 2)  //
        {
            if ((l_sys.Pwp_Open == TRUE) || (l_sys.Sterilize == TRUE) || (l_sys.OutWater_Flag == WATER_NORMAL_ICE))
            {
                req_bitmap_op(DO_UV1_BPOS, 1);  //紫外灯常开
            }
            else
            {
                req_bitmap_op(DO_UV1_BPOS, 0);  //紫外灯关闭
            }
        }
        else if (g_sys.config.ComPara.u16Sterilize_Mode == 3)  //
        {
            if ((l_sys.Pwp_Open == TRUE) || (l_sys.Sterilize == TRUE) || (l_sys.OutWater_Flag == WATER_NORMAL_ICE))
            {
                l_sys.u16UV_Delay = g_sys.config.ComPara.u16UV_Delay * 60;
                req_bitmap_op(DO_UV1_BPOS, 1);  //紫外灯常开
            }
            else
            {
                if (l_sys.u16UV_Delay == 0)
                {
                    req_bitmap_op(DO_UV1_BPOS, 0);  //紫外灯关闭
                }
            }
        }
        else
        {
            req_bitmap_op(DO_UV1_BPOS, 0);  //紫外灯常开
        }
    }
    else
    {
        req_bitmap_op(DO_UV1_BPOS, 0);  //紫外灯常开
    }

    return;
}

//显示控制
void DIS_exe()
{
    return;
}
//杀菌
void Circle_exe(void)
{
    uint16_t u16WL;
    static uint32_t u32Sterilize_Interval[2] = {0};
    static uint16_t u16Sterilize_Time[2]     = {0};
    //  static uint8_t u8Times                   = 0;
    uint8_t u8STR;
    uint16_t u16Temp;

    //外壳打开时，关闭紫外灯
    if ((get_alarm_bitmap(ACL_E4)) || (get_alarm_bitmap(ACL_E28)))
    {
        UV_req_exe(FALSE);              // UV关闭
        req_bitmap_op(DO_P2_BPOS, 0);   //泵2
        req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
        req_bitmap_op(DO_EV1_BPOS, 0);  //循环阀
        return;
    }

    u16Temp = 1;
    //    //出水中
    //    if ((sys_get_remap_status(WORK_MODE_STS_REG_NO, OUTWATER_STS_BPOS) == TRUE)) //Water out
    //    {
    //        return;
    //    }

    if (Exit_Water() != WATER_AIR)  //外接水源
    {
        if (Exit_Water() == WATER_FILL)
        {
        }
        else
        {
            req_bitmap_op(DO_P1_BPOS, 0);  //泵1
        }
        return;
    }

    if (l_sys.Pwp_Open == TRUE)  //收集
    {
        return;
    }

    // 220V紫外灯
    u8STR = 0;
    u32Sterilize_Interval[u8STR]++;

    //贮存
    if (l_sys.u8Storage_Status == TRUE)
    {
        req_bitmap_op(DO_EV1_BPOS, 0);     //循环阀
        req_bitmap_op(DO_P1_BPOS, 0);      //循环泵
        u32Sterilize_Interval[u8STR] = 0;  //贮存后，重新计时
        u16Sterilize_Time[u8STR]     = 0;
        return;
    }
    u16Temp |= 0x02;
    if (((u32Sterilize_Interval[u8STR] >=
          (g_sys.config.ComPara.u16Sterilize_Interval[u8STR] * 60 * OFFSET_DELAY)) ||  //间隔时间到
         (g_sys.config.ComPara.u16CycleStart)) &&                                      //定时循环
        (l_sys.Pwp_Open == FALSE))                                                     //防止与进水泵冲突
    {
        u16Temp |= 0x04;
        u16Sterilize_Time[u8STR]++;
        req_bitmap_op(DO_EV1_BPOS, 1);  //循环阀
        req_bitmap_op(DO_P1_BPOS, 1);   //循环泵
                                        //				if(l_sys.Sterilize == FALSE)
                                        //				{
                                        //						l_sys.Sterilize = TRUE;
                                        //						if(u8Times< CYCLE_TOL)//每3次开一次EV1
                                        //						{
                                        //								u8Times++;
                                        //						}
                                        //						else
                                        //						{
                                        //							            u16Temp |= 0x100;
                                        //							u8Times=0;
                                        //						}
                                        //				}
                                        //				if(u8Times==0)
                                        //				{
                                        //						req_bitmap_op(DO_EV1_BPOS, 1); //出水时会关闭,杀菌时打开
                                        //				}
                                        //        //UV
                                        //        req_bitmap_op(DO_P2_BPOS, 1);  //出水泵

        //        if (g_sys.config.ComPara.u16CloseFrist != PUMP_FIRET)
        //        {
        //            req_bitmap_op(DO_EV2_BPOS, 0); //出水阀
        //        }
        //        else
        //        {
        //            if (l_sys.u8CloseDelay == 0)
        //            {
        //                req_bitmap_op(DO_EV2_BPOS, 0); //出水阀
        //            }
        //        }
    }
    else
    {
        u16Temp |= 0x08;
        if ((l_sys.u8CloseDelay == 0) && (l_sys.u16BD_Time == 0))
        {
            req_bitmap_op(DO_P1_BPOS, 0);  //出水泵
        }
        req_bitmap_op(DO_EV1_BPOS, 0);  //循环阀
    }

    //    if (l_sys.OutWater_Flag )//出水中，防止冲突
    //    {
    //				if(l_sys.Sterilize == TRUE)
    //				{
    //					u16Sterilize_Time[u8STR]--;
    //				}
    //				req_bitmap_op(DO_P2_BPOS, 0); //泵2
    //        req_bitmap_op(DO_EV1_BPOS, 0); //出水时会关闭,杀菌时打开
    //    }
    //		else
    {
        //水位
        u16WL = Get_Water_level();

        if (((u16Sterilize_Time[u8STR] >= g_sys.config.ComPara.u16Sterilize_Time[u8STR] * 60 * OFFSET_DELAY) ||
             ((u16WL & D_L) == 0)) &&
            (l_sys.Pwp_Open == FALSE))
        {
            u16Temp |= 0x10;
            u32Sterilize_Interval[u8STR]       = 0;
            u16Sterilize_Time[u8STR]           = 0;
            l_sys.Sterilize                    = FALSE;
            g_sys.config.ComPara.u16CycleStart = 0;  //关闭循环

            if (g_sys.config.ComPara.u16CloseFrist == PUMP_FIRET)
            {
                req_bitmap_op(DO_P1_BPOS, 0);  //泵2
            }
            else
            {
                if (l_sys.u8CloseDelay == 0)
                {
                    req_bitmap_op(DO_P1_BPOS, 0);  //泵2
                }
            }
            req_bitmap_op(DO_EV1_BPOS, 0);  //循环阀
        }
    }
    return;
}

//除霜
void Defrost_req_exe(void)
{
    int16_t i16NTC1;
    int16_t i16NTC2;

    if (Exit_Water() != WATER_AIR)  //外接水源
    {
        return;
    }

    i16NTC1 = (int16_t)g_sys.status.ComSta.u16Ain[AI_NTC1];
    i16NTC2 = (int16_t)g_sys.status.ComSta.u16Ain[AI_NTC2];

    // set Deforost status
    if (i16NTC1 < (int16_t)g_sys.config.ComPara.u16Start_Defrost_Temp)
    {
        sys_set_remap_status(WORK_MODE_STS_REG_NO, DEFROST1_STS_BPOS, 1);
    }
    else if (i16NTC1 > (int16_t)g_sys.config.ComPara.u16Stop_Defrost_Temp)
    {
        sys_set_remap_status(WORK_MODE_STS_REG_NO, DEFROST1_STS_BPOS, 0);
    }

    if (i16NTC2 < (int16_t)g_sys.config.ComPara.u16Start_Defrost_Temp)
    {
        sys_set_remap_status(WORK_MODE_STS_REG_NO, DEFROST2_STS_BPOS, 1);
    }
    else if (i16NTC2 > (int16_t)g_sys.config.ComPara.u16Stop_Defrost_Temp)
    {
        sys_set_remap_status(WORK_MODE_STS_REG_NO, DEFROST2_STS_BPOS, 0);
    }

    return;
}

//制冰水
void Cold_Water_exe(void)
{
    return;
}

//扇热风机
void Heat_Fan_exe(void)
{
    return;
}

uint8_t Sys_Get_Storage_Signal(void)
{
    uint8_t ret;

    if (g_sys.config.ComPara.u16Storage == 1)
    {
        ret = TRUE;
    }
    else
    {
        ret = FALSE;
    }
    return ret;
}

//贮存
void Storage_exe(void)
{
    uint8_t u8Storage_Fsm;
    uint16_t u16BUff6 = 0;
    uint16_t u16WL;

    u16WL = Get_Water_level();  //水位

    u8Storage_Fsm = l_sys.u8Storage_Fsm;
    if (Sys_Get_Storage_Signal() == TRUE)
    {
        u16BUff6 |= 0x01;
        l_sys.u8Storage_Status = TRUE;
        l_sys.Pwp_Open         = FALSE;
        l_sys.Sterilize        = FALSE;
    }
    else
    {
        l_sys.u8Storage_Status = FALSE;
    }
    switch (u8Storage_Fsm)
    {
        case WATER_STROGE_IDLE: {
            if ((l_sys.u8Storage_Status == TRUE) &&
                ((sys_get_remap_status(WORK_MODE_STS_REG_NO, FAN_STS_BPOS) == 0) &&
                 (sys_get_remap_status(WORK_MODE_STS_REG_NO, COOLING_STS_BPOS) == 0)))
            {
                u16BUff6 |= 0x02;
                l_sys.comp_timeout[DO_P1_BPOS] = g_sys.config.ComPara.u16StorageDealy[1];
                l_sys.u8Storage_Fsm            = WATER_STROGE_1;
            }
            else
            {
                u16BUff6 |= 0x04;
            }
        }
        break;
        case WATER_STROGE_1:  //开收集泵,EV2，运行10s
        {
            if (l_sys.u8Storage_Status == TRUE)
            {
                if (u16WL & S_L)
                {
                    u16BUff6 |= 0x08;
                    req_bitmap_op(DO_P1_BPOS, 1);   //开启收集泵
                    req_bitmap_op(DO_EV3_BPOS, 1);  //
                    req_bitmap_op(DO_EV2_BPOS, 1);  //出水阀
                    l_sys.comp_timeout[DO_P1_BPOS] = g_sys.config.ComPara.u16StorageDealy[1];
                }
                else
                {
                    if (l_sys.comp_timeout[DO_P1_BPOS])
                    {
                        u16BUff6 |= 0x10;
                        req_bitmap_op(DO_P1_BPOS, 1);   //开启收集泵
                        req_bitmap_op(DO_EV3_BPOS, 1);  //
                        req_bitmap_op(DO_EV2_BPOS, 1);  //出水阀
                    }
                    else
                    {
                        u16BUff6 |= 0x20;
                        req_bitmap_op(DO_P1_BPOS, 0);   //收集泵
                        req_bitmap_op(DO_EV3_BPOS, 0);  //
                        req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀

                        l_sys.comp_timeout[DO_EV1_BPOS] = g_sys.config.ComPara.u16StorageDealy[0];
                        l_sys.u8Storage_Fsm             = WATER_STROGE_2;
                    }
                }
            }
            else
            {
                req_bitmap_op(DO_P1_BPOS, 0);   //收集泵
                req_bitmap_op(DO_EV3_BPOS, 0);  //
                req_bitmap_op(DO_EV2_BPOS, 0);  //出水阀
                l_sys.u8Storage_Fsm    = WATER_STROGE_IDLE;
                l_sys.u8Storage_Status = FALSE;
            }
        }
        break;
        case WATER_STROGE_2:  //开EV1，运行80s
        {
            if (l_sys.u8Storage_Status == TRUE)
            {
                if (u16WL & D_L)
                {
                    u16BUff6 |= 0x40;
                    req_bitmap_op(DO_EV2_BPOS, 1);  //
                    req_bitmap_op(DO_P1_BPOS, 1);   //
                    req_bitmap_op(DO_EV1_BPOS, 1);  //
                    l_sys.comp_timeout[DO_EV1_BPOS] = g_sys.config.ComPara.u16StorageDealy[0];
                }
                else  //缺水
                {
                    if (l_sys.comp_timeout[DO_EV1_BPOS])
                    {
                        u16BUff6 |= 0x80;
                        req_bitmap_op(DO_EV2_BPOS, 1);  //
                        req_bitmap_op(DO_P1_BPOS, 1);   //
                        req_bitmap_op(DO_EV1_BPOS, 1);  //
                    }
                    else
                    {
                        u16BUff6 |= 0x100;
                        req_bitmap_op(DO_EV2_BPOS, 0);  //
                        req_bitmap_op(DO_P1_BPOS, 0);   //
                        req_bitmap_op(DO_EV1_BPOS, 0);  //
                        l_sys.u8Storage_Fsm = WATER_STROGE_3;
                    }
                }
            }
            else
            {
                req_bitmap_op(DO_EV2_BPOS, 0);  //
                req_bitmap_op(DO_P1_BPOS, 0);   //
                req_bitmap_op(DO_EV1_BPOS, 0);  //
                l_sys.u8Storage_Fsm    = WATER_STROGE_IDLE;
                l_sys.u8Storage_Status = FALSE;
            }
        }
        break;
        case WATER_STROGE_3:  //清滤芯
        {
            if (l_sys.u8Storage_Status == TRUE)
            {
                u16BUff6 |= 0x200;
                reset_runtime(DO_FILLTER_ELEMENT_DUMMY_BPOS_0);
                reset_runtime(DO_FILLTER_ELEMENT_DUMMY_BPOS_1);
                reset_runtime(DO_FILLTER_ELEMENT_DUMMY_BPOS_2);
                reset_runtime(DO_FILLTER_ELEMENT_DUMMY_BPOS_3);
                l_sys.u8Storage_Fsm = WATER_STROGE_STOP;
            }
            else
            {
                l_sys.u8Storage_Fsm    = WATER_STROGE_IDLE;
                l_sys.u8Storage_Status = FALSE;
            }
        }
        break;
        case WATER_STROGE_STOP: {
            u16BUff6 |= 0x400;
            g_sys.config.ComPara.u16Storage = 0;
            RAM_Write_Reg(EE_STORAGE, g_sys.config.ComPara.u16Storage, 1);
            l_sys.u8Storage_Fsm    = WATER_STROGE_IDLE;
            l_sys.u8Storage_Status = FALSE;
        }
        break;
        default: {
            l_sys.u8Storage_Fsm    = WATER_STROGE_IDLE;
            l_sys.u8Storage_Status = FALSE;
        }
        break;
    }
    // 		rt_kprintf("u16BUff6=%x,u16WL=%x,u8Stats_Storage=%x,u8Storage_Fsm=%d,u16Storage=%d,Pwp_Open=%d,Sterilize=%d\n",
    // u16BUff6,u16WL,l_sys.u8Storage_Status,
    // l_sys.u8Storage_Fsm,g_sys.config.ComPara.u16Storage,l_sys.Pwp_Open,l_sys.Sterilize);
    return;
}

////显示器控制
// uint8_t Close_DIS_Enable(void)
//{
//
//
//
//		//禁止重启
//		if((g_sys.config.Platform.Restart_Enable&0x8000)&&(sys_get_di_sts(DI_HI_PRESS2_BPOS)))
//		{
//			return TRUE;
//		}
//		else
//		{
//			return FALSE;
//		}
//}

////显示器控制
// void Restart_DIS_exe(void)
//{
//
//

//    static uint8_t u8CloseTime[2] = {0};
//    static uint16_t u16NeterrDelay = 0;
//    static uint32_t u32RSInteral = 0;
//    static uint8_t u8Restart=0;
//    uint16_t u16MaxTimes;
//    uint16_t u16HoldTime;
//    uint32_t u32InteralTime;
//
//		u16MaxTimes=g_sys.config.Platform.Restart_Enable&0x0FFF;
////		u16MaxTimes=3;
//		u32InteralTime=(g_sys.config.Platform.Restart_Delay>>8)*3600*2;
//		u16HoldTime=(g_sys.config.Platform.Restart_Delay&0x00FF)*60*2;
//// 		rt_kprintf("u8CloseTime[1]=%d,u32RSInteral=%d\n", u8CloseTime[1],u32RSInteral);
////		u16HoldTime=100;

//		//禁止重启
//		if(Close_DIS_Enable()==FALSE)
//		{
//			u8CloseTime[1]=0;
//			u32RSInteral=0;
//			l_sys.u16Uart_Timeout[1]=0;
//			u16NeterrDelay=0;
//			u8Restart=0;
//			l_sys.u8RSInteral_Neterr=FALSE;
//			Close_DIS_PWR(0);	//开启
//			return;
//		}
//		else
//		{
//				//超过次数
//				if(u8CloseTime[1]>=u16MaxTimes)
//				{
//						u32RSInteral++;
//						if(u32RSInteral>=u32InteralTime)//间隔时间
//						{
//								u32RSInteral=0;
//								u8CloseTime[1]=0;
//						}

//						l_sys.u8RSInteral_Neterr=TRUE;//网络异常
//						l_sys.u16Uart_Timeout[1]=0;
//						u16NeterrDelay=0;
//						u8Restart=0;
//						Close_DIS_PWR(0);
//						return;
//				}
//				else
//				{
//						//APP可能退出，通信异常
//						l_sys.u16Uart_Timeout[1]++;
//						if(l_sys.u16Uart_Timeout[1]>=u16HoldTime)
//						{
//								l_sys.u16Uart_Timeout[1]=u16HoldTime;
//								u8Restart|=0x01;
//						}

//						if(g_sys.config.Platform.Net_ERR!=0) //网络异常
//						{
//								u16NeterrDelay++;
//								if(u16NeterrDelay>=u16HoldTime)
//								{
//										u16NeterrDelay=u16HoldTime;
//										u8Restart|=0x03;
//								}
//						}

//						l_sys.u8RSInteral_Neterr=FALSE;

//				}
//				//需要重启
//				if(u8Restart)
//				{
//						u8CloseTime[0]++;
//						if(u8CloseTime[0]<=CLOSE_TIME)
//						{
//							Close_DIS_PWR(1);//关闭XX秒
//						}
//						else
//						{
//							u8CloseTime[0]=0;
//							u8CloseTime[1]++;//次数
//							l_sys.u16Uart_Timeout[1]=0;
//							u16NeterrDelay=0;
//							g_sys.config.Platform.Net_ERR=0;
//							u8Restart=0;
//						}
//
//				}
//				else
//				{
//							Close_DIS_PWR(0);
//				}
//		}
//// 		rt_kprintf("u8CloseTime[0]=%x,u8CloseTime[1]=%x,u16Uart_Timeout[1]=%x,u8Restarterr=%d,DI_HI_PRESS2=%d\n",
/// u8CloseTime[0],u8CloseTime[1],l_sys.u16Uart_Timeout[1],u8Restart, sys_get_di_sts(DI_HI_PRESS2_BPOS));

//    return;
//}

//制水需求
void Product_req_exe(int16_t target_req_temp, int16_t target_req_hum)
{
    //风机,压机启动条件
    Sys_Fan_CP_WL();
    //风机控制
    fan_req_exe(target_req_temp, target_req_hum);
    //加热器控制
    Heat_req_exe(target_req_temp, target_req_hum);
    //    //压缩机控制
    //    compressor_req_exe(target_req_temp, target_req_hum);
    return;
}

//水路需求
void Water_req_exe(void)
{
    //收集
    Collect_exe();
    //    //制冰水
    //    Cold_Water_exe();
    //循环杀菌
    Circle_exe();
    //出水
    WaterOut_req_exe();
    //    //除霜
    //    Defrost_req_exe();
    //贮存
    Storage_exe();
    // UV开启
    UV_req_exe(TRUE);
    //显示屏控制
    DIS_exe();
    return;
}

void reqCounter500ms(void)
{
    static rt_uint8_t childLockCounter = 0;
    if (l_sys.LedKey.ChildLock)
    {
        if (childLockCounter == 0)
        {
            childLockCounter = g_sys.config.ComPara.t12ChildLockTime * 2;
        }
        if (childLockCounter > 0)
        {
            childLockCounter--;
            if (childLockCounter == 0)
            {
                l_sys.LedKey.ChildLock = FALSE;
            }
        }
    }
    else
    {
        childLockCounter = 0;
    }
}

//总体需求执行逻辑
void req_execution(int16_t target_req_temp, int16_t target_req_hum)
{
    //制水
    Product_req_exe(target_req_temp, target_req_hum);
    //水路
    Water_req_exe();

    reqCounter500ms();

    return;
}
